calloop
Calloop, a Callback-based Event Loop
This crate provides an EventLoop
type, which is a small abstraction
over a polling system. The main difference between this crate
and other traditional rust event loops is that it is based on callbacks:
you can register several event sources, each being associated with a callback
closure that will be invoked whenever the associated event source generates
events.
The main target use of this event loop is thus for apps that expect to spend most of their time waiting for events and wishes to do so in a cheap and convenient way. It is not meant for large scale high performance IO.
How to use it
For simple uses, you can just add event sources with callbacks to the event loop. For example, here's a runnable program that exits after five seconds:
use ;
Event source types
The event loop is backed by an OS provided polling selector (epoll on Linux).
This crate also provide some adapters for common event sources such as:
- MPSC channels
- Timers
- unix signals on Linux
As well as generic objects backed by file descriptors.
It is also possible to insert "idle" callbacks. These callbacks represent computations that
need to be done at some point, but are not as urgent as processing the events. These callbacks
are stored and then executed during EventLoop::dispatch
, once all events from the sources
have been processed.
Async/Await compatibility
calloop
can be used with futures, both as an executor and for monitoring Async IO.
Activating the executor
cargo feature will add the futures
module, which provides
a future executor that can be inserted into an EventLoop
as yet another EventSource
.
IO objects can be made Async-aware via the LoopHandle::adapt_io
method. Waking up the
futures using these objects is handled by the associated EventLoop
directly.
Custom event sources
You can create custom event sources can will be inserted in the event loop by
implementing the EventSource
trait. This can be done either directly from the file
descriptors of your source of interest, or by wrapping an other event source and further
processing its events. An EventSource
can register more than one file descriptor and
aggregate them.
Platforms support
Currently, only Linux and the *BSD are supported.
License: MIT